package com.pingtai.xinlang.login.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.pingtai.dao.BaseDao;
import com.pingtai.xinlang.login.entity.LoginUser;
import com.pingtai.xinlang.login.entity.XinlangLoginCookie;

public class XinlangDao extends BaseDao{

	public String getPassByUserName(String userName){
		Session session = getSession();
		Query query = session.createQuery("from LoginUser  where name=:name");
		query.setParameter("name", userName);
		List list = query.list();
		if(list!=null&&list.size()>0){
			return (String) list.get(0);
		}
		return null;
	}
	
	public LoginUser getLoginUserByName(String name){
		Session session = getSession();
		Query query = session.createQuery("from LoginUser  where name=:name");
		query.setParameter("name", name);
		List list = query.list();
		if(list!=null&&list.size()>0){
			return (LoginUser) list.get(0);
		}
		return null;
	}
	
	public LoginUser getLastUseLoginUser(Integer id){
		Session session = getSession();
		Query query = session.createQuery("from LoginUser where id=:id  order by lastLoginDate asc");
		query.setParameter("id", id);
		List list = query.list();
		if(list!=null&&list.size()>0){
			return (LoginUser) list.get(0);
		}
		return null;
	}
	
	
	
	public XinlangLoginCookie getLoginCookieByNameAndDomain
					(String name,String domain,LoginUser loginUser){
		Session session = getSession();
		Query query = session.createQuery("from XinlangLoginCookie  where name=:name and domain=:domain and loginUser=:loginUser");
		query.setParameter("name", name);
		query.setParameter("domain", domain);
		query.setParameter("loginUser", loginUser);
		List list = query.list();
		if(list!=null&&list.size()>0){
			return (XinlangLoginCookie) list.get(0);
		}
		return null;
	}
	
	public List getLoginCookiesByUser(LoginUser user){
		Session session = getSession();
		Query query = session.createQuery("from XinlangLoginCookie  where  loginUser=:loginUser");
		query.setParameter("loginUser", user);
		List list = query.list();
		return list;
	}
	

}
